package com.message.api.group.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.annotation.SysLog;
import com.base.context.Context;
import com.base.context.ContextHolder;
import com.base.helper.BaseController;
import com.base.helper.Result;
import com.message.api.group.entity.AcGroupAction;
import com.message.api.group.service.AcGroupActionService;
import com.message.api.group.vo.PurviewGrantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Api(tags = "角色功能")
@RestController
@RequestMapping("/acGroupAction")
public class AcGroupActionController extends BaseController {

    private static final Logger               LOG = LoggerFactory.getLogger(AcGroupActionController.class);
    @Autowired
    private              AcGroupActionService entityService;

    @SysLog(action = "acGroupAction:findByPage", value = "角色管理：角色功能列表")
    @ApiOperation(value = "分页查询角色功能", notes = "分页查询角色功能")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面：从1开始", paramType = "query")})
    public Result<IPage<AcGroupAction>> findByPage(final AcGroupAction example, final Page page) {
        IPage<AcGroupAction> records = entityService.page(example, page);
        return new Result(true, records);
    }

    @SysLog(action = "acGroupAction:delete", value = "角色管理：删除角色功能")
    @ApiOperation(value = "删除角色功能")
    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "角色功能ID", required = true, paramType = "path")
    public Result delete(@PathVariable final Integer id) {
        AcGroupAction acGroupAction = entityService.getById(id);
        if (null == acGroupAction) {
            throw new RuntimeException("没有找到id对应对象");
        }
        acGroupAction.setState("0");
        entityService.updateById(acGroupAction);
        return new Result(true, "成功删除角色功能", null);
    }

    @SysLog(action = "acGroupAction:one", value = "角色管理：查询角色功能详细")
    @ApiOperation(value = "查询单个角色功能")
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "角色功能ID", required = true, paramType = "path")
    public Result one(@PathVariable final Integer id) {
        AcGroupAction entity = entityService.getById(id);
        return new Result(true, entity);
    }

    @SysLog(action = "acGroupAction:add", value = "角色管理：添加角色功能")
    @ApiOperation(value = "添加角色功能")
    @RequestMapping(method = {RequestMethod.POST})
    public Result add(@Valid @RequestBody final AcGroupAction entity) {
        if (null != entity.getId()) {
            entity.setCreatetime(new Date());
            entity.setState("1");
        }
        entityService.save(entity);
        return new Result(true, "成功保存角色功能", null);
    }

    @SysLog(action = "acGroupAction:update", value = "角色管理：修改角色功能")
    @ApiOperation(value = "修改角色功能")
    @RequestMapping(method = {RequestMethod.PUT})
    public Result update(@Valid @RequestBody final AcGroupAction entity) {
        if (null == entity.getId()) {
            throw new RuntimeException("id不能为空");
        }
        entityService.updateById(entity);
        return new Result(true, "成功修改角色功能", null);
    }

    @SysLog(action = "acGroupAction:all", value = "角色管理：全部角色功能")
    @ApiOperation(value = "全部角色功能")
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Result<List<AcGroupAction>> all(AcGroupAction example) {
        List<AcGroupAction> entitys = entityService.list(example);
        if (null != entitys) {
            return new Result<>(true, entitys);
        }
        return new Result<>(true, Collections.emptyList());
    }

    @SysLog(action = "acGroupAction:purviewsOf", value = "角色管理：查询角色拥有的功能")
    @ApiOperation(value = "查询角色拥有的功能")
    @RequestMapping(value = "purviews/of/{groupid}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "groupid", value = "角色ID", required = true, paramType = "path")
    public Result<List<String>> purviewsOf(@PathVariable("groupid") final Integer groupid) {
        List<AcGroupAction> groupActions = entityService.findByGroupid(groupid);
        return new Result<>(true, groupActions.stream().map(AcGroupAction::getAction).collect(Collectors.toList()));
    }

    @SysLog(action = "acGroupAction:grantPurviews", value = "角色管理：设置角色的权限")
    @ApiOperation(value = "设置角色的权限")
    @RequestMapping(value = "grantPurviews", method = RequestMethod.POST)
    public Result<Boolean> grantPurviews(@Valid @RequestBody PurviewGrantVo purviewGrant) {
        Context context = ContextHolder.currentContext();
        entityService.grantPurviews(context, purviewGrant);
        return new Result<>(true, "权限设置成功", null, true);
    }

}

